package com.zywl.utils.utils;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具类
 */
public class RegexUtil {
    private static final String BLANK_SPACE = "(\\u00A0|\\u0020|\\u3000|\\u007F)*";
    private static final Pattern FIGURE = Pattern.compile("[0-9]*");
    private static final Pattern CHINESE_CONTAIN = Pattern.compile("[\u4e00-\u9fa5]");
    private static final Pattern FIGURE_CONTAIN = Pattern.compile(".*\\d+.*");

    /**
     * 判断字符串中是否包含中文
     *
     * @param str
     * @return
     */
    public static boolean isContainChinese(String str) {
        Matcher m = CHINESE_CONTAIN.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串中是否包含数字
     *
     * @param str
     * @return
     */
    public static boolean isContainNumber(String str) {
        Matcher m = FIGURE_CONTAIN.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串中是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        if (StringUtils.isNotEmpty(str)) {
            Matcher m = FIGURE.matcher(str);
            if (m.matches()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 去除所有空格
     *
     * @param str
     * @return
     */
    public static String replaceBlankSpace(String str) {
        if (StringUtils.isNotEmpty(str)) {
            str = str.replaceAll(BLANK_SPACE, Constants.STRING_NULL);
            return str;
        }
        return null;
    }
}
